package com.dongnao.json.fast;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 属性详情
 * @author Lance
 * @date 2018/5/7
 */

public class FieldInfo {

    /** 属性名字*/
    public String name;
    /** 属性*/
    public Field field;
    /** 属性对应的方法*/
    public Method method;
    /** 属性类型*/
    public Class type;

    public FieldInfo(String name, Method method, Field field) {
        this.name = name;
        this.field = field;
        this.method = method;
        type = method != null ? method.getReturnType() : field.getType();
    }

    public FieldInfo(String name, Method method, Field field, Class type) {
        this.name = name;
        this.field = field;
        this.method = method;
        this.type = type;
    }

    /**
     *  若method存在则调用该方法，如果获取field属性值
     *  (此处输入方法执行任务.)
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2018/5/15,19:13
     * <h3>UpdateTime</h3> 2018/5/15,19:13
     * <h3>CreateAuthor</h3> luzhenbang
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param object 需要序列化的对象，method的所属的类
     * @return field（method） 对应的值
     *
     */
    public Object get(Object object) {
        try {
            return method != null ? method.invoke(object) : field.get(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    public void set(Object object, Object value) {
        try {
            if (method != null) {
                method.invoke(object, value);
            } else {
                field.set(object, value);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }



}
